/**AJAX请求处理构建实例
 *
 * Configure：参数对象
 * EnableAsync：启用异步请求模式，IE浏览器下默认强制为false，不启用
 * TimeOut：设置请求超时的时间
 * 示例：
 * var test= AJAX_RequestProcess({EnableAsync:true,TimeOut:15000});
 *
 * 本实例共有GetRequest（Get 请求）、PostRequest（Post 请求）、abortAnyRequest（中断请求）三个函数
 * 用法：
 *
 * GET
 * test.GetRequest(需要请求的网址,请求体数据,回调函数,额外的请求头对象,响应类型);
 *
 * POST
 * test.PostRequest(需要请求的网址,请求体数据,回调函数,额外的请求头对象,响应类型);
 *
 * 中断请求
 * test.abortAnyRequest();
 *
 * 回调函数的传参在异步请求模式下是一个包含 value、status、processCode 属性的对象，value：服务端响应返回的数据，status：响应状态对象，包含有code、text属性
 * code：响应状态码，text：响应状态文本，processCode：请求的状态码，与旧版的请求状态对应。
 * 非异步模式下传参是请求后得到的响应数据对象。
 *
 * 示例：test.GetRequest(需要请求的网址,请求体数据,function(value){
 *     switch(value.processCode){
 *             case "ready":
 *             console.log("这是请求成功的");
 *             break;
 *             case "abort":
 *             console.log("这是请求放弃的");
 *             break;
 *             case "error":
 *             console.log("这是请求错误的");
 *             break;
 *             case "timeout":
 *             console.log("这是请求超时的");
 *             break;
 *      }
 * })
 *
 * 非异步模式下可这样获取响应数据
 *
 * var pk = test.PostRequest(需要请求的网址,请求体数据);
 *
 * 以上示例均可在 GetRequest、PostRequest 函数中通用
 *
 * 本AJAX请求处理构建实例函数可完美兼容至 Internet Explorer 6.0 （IE6）浏览器使用
 *
 * */
function AJAX_RequestProcess(Configure) {
    var InstanceObject = {
        __XMLInstance: undefined,
        __config: {
            timeout: 0,
            EnableAsync: false,
            BrowseEnviron: "IE",
            withCredentials: false,
            userName: "",
            passWord: ""
        },
        __Tools: {
            objectConvertPostBody: function (objectData) {
                var FinalConvert = [];
                if (Object.prototype.toString.call(objectData) === "[object Object]") {
                    function DataMatchProcess(source) {
                        var Origin;
                        var confirmValue;
                        var confirmItem;
                        var Final;
                        switch (Object.prototype.toString.call(source)) {
                            case "[object String]":
                                return source;
                            case "[object Number]":
                                if (isFinite(source) && !isNaN(source)) {
                                    return source;
                                } else {
                                    return "";
                                }
                            case "[object Object]":
                                Origin = ["{"];
                                var count = 0;
                                for (var item in source) {
                                    confirmValue = DataMatchProcess(source[item]);
                                    if (String(confirmValue).length > 0) {
                                        count++;
                                        confirmItem = "\"" + item + "\"";
                                        Origin.push(confirmItem)
                                        Origin.push(":")
                                        Origin.push(confirmValue);
                                        Origin.push(",");
                                    }
                                }
                                if (count > 0) {
                                    Origin.pop();
                                }
                                Origin.push("}");
                                return Origin.join("");
                            case "[object Array]":
                                Origin = ["["];
                                if (source.length > 0) {
                                    for (var item in source) {
                                        confirmValue = DataMatchProcess(source[item]);
                                        if (Origin.length > 1) {
                                            Origin.push(",");
                                        }
                                        switch (Object.prototype.toString.call(confirmValue)) {
                                            case "[object String]":
                                                confirmValue = "\"" + confirmValue + "\"";
                                                Origin.push(confirmValue);
                                                break;
                                            default:
                                                Origin.push(confirmValue);
                                                break;
                                        }
                                    }
                                    Origin.push("]");
                                } else {
                                    Origin.push("]");
                                }
                                return Origin.join("");
                            case "[object Boolean]":
                                if (source) {
                                    return "true";
                                } else {
                                    return "false";
                                }
                            case "[object Uint8Array]":
                                Origin = {
                                    type: "Uint8Array",
                                    data: []
                                }
                                for (var item = 0; item < source.byteLength; item++) {
                                    Origin.data.push(source[item]);
                                }
                                Final = DataMatchProcess(Origin);
                                return Final;
                            case "[object ArrayBuffer]":
                                Origin = {
                                    type: "ArrayBuffer",
                                    data: []
                                }
                                for (var item in source) {
                                    Origin.data.push(source[item]);
                                }
                                Final = DataMatchProcess(Origin);
                                return Final;
                            default:
                                return "";
                        }
                    }

                    for (var num in objectData) {
                        if (FinalConvert.length > 0) {
                            FinalConvert.push("&");
                        }
                        FinalConvert.push(num);
                        FinalConvert.push("=");
                        FinalConvert.push(DataMatchProcess(objectData[num]));
                    }
                }
                return FinalConvert.join("");
            },
            requestHeaderProcess: function (HeaderObject) {
                if (Object.prototype.toString.call(HeaderObject) === "[object Object]" && this.__Tools.detectObject(HeaderObject)) {
                    for (var item in HeaderObject) {
                        this.__XMLInstance.setRequestHeader(item, HeaderObject[item]);
                    }
                }
            },
            detectObject: function (source) {
                var Final = {length: 0, PropertyItem: []}
                try {
                    for (var item in source) {
                        Final.length++;
                        Final.PropertyItem.push(item);
                    }
                } catch (e) {
                    return false;
                }
                return Final;
            },
            ResBuilder: function () {
                return {
                    value: undefined,
                    status: {code: undefined, text: undefined},
                    processCode: "ready",
                    responseHeader: {}
                }
            },
            eventInitial: function (curIns, option) {
                function union(value, eventName) {
                    var result = curIns.__Tools.ResBuilder();
                    result.Origin = value;
                    result.processCode = eventName;
                    result.status.code = this.status;
                    result.status.text = this.statusText;
                    result.value = {
                        readyState: undefined,
                        response: undefined,
                        responseText: undefined,
                        responseType: undefined,
                        responseURL: undefined,
                        responseXML: undefined,
                        status: undefined,
                        statusText: undefined
                    };
                    result.value.readyState = this.readyState;
                    result.value.response = this.response;
                    result.value.responseText = this.responseText;
                    result.value.responseType = this.responseType;
                    result.value.responseURL = this.responseURL;
                    result.value.responseXML = this.responseXML;
                    result.value.status = this.status;
                    result.value.statusText = this.statusText;
                    result.responseHeader = curIns.__Tools.getResponseHeaderInfo.call(curIns);
                    if (
                        Object.prototype.toString.call(option.CallBack) === "[object Function]"
                        ||
                        Object.prototype.toString.call(option.CallBack) === "[object AsyncFunction]"
                    ) {
                        option.CallBack(result);
                    }
                }

                this.__XMLInstance.onabort = function (value) {
                    union.call(this, value, "onabort");
                };
                this.__XMLInstance.onerror = function (value) {
                    union.call(this, value, "onerror");
                };
                this.__XMLInstance.onload = function (value) {
                    union.call(this, value, "onload");
                };
                this.__XMLInstance.onloadend = function (value) {
                    union.call(this, value, "onloadend");
                };
                this.__XMLInstance.onloadstart = function (value) {
                    union.call(this, value, "onloadstart");
                };
                this.__XMLInstance.onprogress = function (value) {
                    union.call(this, value, "onprogress");
                };
                this.__XMLInstance.ontimeout = function (value) {
                    union.call(this, value, "ontimeout");
                };
                this.__XMLInstance.onreadystatechange = function (value) {
                    union.call(this, value, "onreadystatechange");
                };
            },
            lightCopy: function (source) {
                return source;
            },
            addressParamSplit: function (source) {
                var FinalSplit = [];
                if (Object.prototype.toString.call(source) === "[object String]" && String(source).length >= 2) {
                    if (String(source).indexOf("/") < 0) {
                        return false;
                    } else {
                        function ParamDetect(source) {
                            var dySymCount = 0;
                            var gtSymCount = 0;
                            for (var strPos = 0; strPos < String(source).length; strPos++) {
                                if (String(source).charAt(strPos) === "=") {
                                    dySymCount++;
                                }
                                if (String(source).charAt(strPos) === "&") {
                                    gtSymCount++;
                                }
                            }
                            var splitGroup;
                            if (gtSymCount > 0) {
                                splitGroup = String(source).split("&");
                                for (var arrChk = 0; arrChk < splitGroup.length; arrChk++) {
                                    if (String(splitGroup[arrChk]).indexOf("=") <= 0) {
                                        return false;
                                    } else {
                                        var pN = String(splitGroup[arrChk]).substring(0, String(splitGroup[arrChk]).indexOf("="));
                                        var pV = String(splitGroup[arrChk]).substring(String(splitGroup[arrChk]).indexOf("=") + 1);
                                        if (String(pV).length === 0) {
                                            return false;
                                        } else if (String(pN).length === 0) {
                                            return false;
                                        }
                                    }
                                }
                                return true;
                            } else {
                                if (dySymCount > 0) {
                                    var sp1 = String(source).substring(0, String(source).indexOf("="));
                                    var sp2 = String(source).substring(String(source).indexOf("=") + 1);
                                    if (String(sp1).length === 0) {
                                        return false;
                                    } else if (String(sp2).length === 0) {
                                        return false;
                                    }
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        }

                        if (String(source).indexOf("?") > -1) {
                            var Address;
                            var Param;
                            for (var pos = 0; pos < String(source).length; pos++) {
                                if (String(source).charAt(pos) === "?") {
                                    Address = String(source).substring(0, pos);
                                    Param = String(source).substring(pos + 1);
                                    break;
                                }
                            }
                            FinalSplit.push(Address);
                            if (ParamDetect(Param)) {
                                FinalSplit.push(Param);
                            }
                        } else {
                            FinalSplit.push(source);
                        }
                        if (FinalSplit.length > 0) {
                            return FinalSplit;
                        } else {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            },
            getResponseHeaderInfo: function () {
                var resHeader = {}
                var OriginResponseHeader = this.__XMLInstance.getAllResponseHeaders();
                var rhArr = String(OriginResponseHeader).split("\r\n");
                for (var len = 0; len < rhArr.length; len++) {
                    if (String(rhArr[len]).toString() !== "") {
                        var rhItem = String(rhArr[len]).split(": ");
                        resHeader[rhItem[0]] = this.__Tools.lightCopy(rhItem[1]);
                    }
                }
                return resHeader;
            },
            RequestProcess: function (option) {
                var confirmOption = {
                    Address: "",
                    Content: "",
                    ContentType: "",
                    ReqType: "get",
                    ResType: "text",
                    SendWays: "auto",
                    Async: false,
                    ExtraHeader: {},
                    CallBack: undefined,
                    UserName: undefined,
                    PassWord: undefined
                }
                if (
                    Object.prototype.toString.call(this.__config) === "[object Object]"
                    &&
                    Object.prototype.toString.call(this.__Tools) === "[object Object]"
                    &&
                    Object.prototype.toString.call(this.__XMLInstance) === "[object Object]"
                    ||
                    Object.prototype.toString.call(this.__XMLInstance) === "[object XMLHttpRequest]"
                ) {
                    confirmOption.Async = this.__config.EnableAsync;
                    confirmOption.UserName = this.__config.userName;
                    confirmOption.PassWord = this.__config.passWord;
                    if (this.__Tools.detectObject(option) && this.__Tools.detectObject(option).length > 0) {
                        for (var initItem in option) {
                            switch (String(initItem).toLowerCase()) {
                                case "address":
                                    confirmOption.Address = this.__Tools.lightCopy(option[initItem]);
                                    break;
                                case "content":
                                    confirmOption.Content = this.__Tools.lightCopy(option[initItem]);
                                    break;
                                case "reqtype":
                                    confirmOption.ReqType = this.__Tools.lightCopy(option[initItem]);
                                    break;
                                case "restype":
                                    confirmOption.ResType = this.__Tools.lightCopy(option[initItem]);
                                    break;
                                case "sendways":
                                    switch (String(option[initItem]).toLowerCase()) {
                                        case "query":
                                            confirmOption.SendWays = "query";
                                            break;
                                        case "body":
                                            confirmOption.SendWays = "body";
                                            break;
                                        default:
                                            confirmOption.SendWays = "auto";
                                            break;
                                    }
                                    break;
                                case "async":
                                    if (Object.prototype.toString.call(option[initItem]) === "[object Boolean]") {
                                        confirmOption.Async = this.__Tools.lightCopy(option[initItem]);
                                    } else {
                                        confirmOption.Async = false;
                                    }
                                    break;
                                case "extraheader":
                                    if (Object.prototype.toString.call(option[initItem]) === "[object Object]" && this.__Tools.detectObject(option[initItem]).length > 0) {
                                        confirmOption.ExtraHeader = this.__Tools.lightCopy(option[initItem]);
                                    }
                                    break;
                                case "callback":
                                    switch (Object.prototype.toString.call(option[initItem])) {
                                        case "[object Function]":
                                            confirmOption.CallBack = this.__Tools.lightCopy(option[initItem]);
                                            break;
                                        case "[object AsyncFunction]":
                                            confirmOption.CallBack = this.__Tools.lightCopy(option[initItem]);
                                            break;
                                    }
                                    break;
                                case "username":
                                    if (String(option[initItem]).length > 0) {
                                        confirmOption.UserName = this.__Tools.lightCopy(String(option[initItem]).toString());
                                    } else {
                                        confirmOption.UserName = null;
                                    }
                                    break;
                                case "password":
                                    if (String(option[initItem]).length > 0) {
                                        confirmOption.PassWord = this.__Tools.lightCopy(String(option[initItem]).toString());
                                    } else {
                                        confirmOption.PassWord = null;
                                    }
                                    break;
                            }
                        }
                        var FinalResponse = this.__Tools.ResBuilder();
                        var curIns = this;
                        var collAddress;
                        var reqBody;
                        var convertContent;
                        var addressAnalyse = this.__Tools.addressParamSplit(confirmOption.Address);
                        if (!addressAnalyse) {
                            throw new Error("地址异常");
                        }

                        function readyChangeProcess() {
                            FinalResponse.responseHeader = curIns.__Tools.getResponseHeaderInfo.call(curIns);
                            FinalResponse.status.code = curIns.__XMLInstance.status;
                            FinalResponse.status.text = curIns.__XMLInstance.statusText;
                            FinalResponse.value = {
                                responseBody: [],
                                responseText: undefined,
                                responseStream: {}
                            };
                            FinalResponse.value.responseText = curIns.__Tools.lightCopy(curIns.__XMLInstance.responseText);
                            for (var item in curIns.__XMLInstance.responseBody) {
                                FinalResponse.value.responseBody.push(curIns.__XMLInstance.responseBody[item]);
                            }
                            for (var item in curIns.__XMLInstance.responseStream) {
                                FinalResponse.value.responseStream[item] = curIns.__Tools.lightCopy(curIns.__XMLInstance.responseStream[item]);
                            }
                            if (
                                Object.prototype.toString.call(confirmOption.CallBack) === "[object Function]"
                                ||
                                Object.prototype.toString.call(confirmOption.CallBack) === "[object AsyncFunction]"
                            ) {
                                confirmOption.CallBack(FinalResponse);
                            }
                        }

                        switch (Object.prototype.toString.call(confirmOption.Content)) {
                            case "[object FormData]":
                                confirmOption.ContentType = "multipart/form-data";
                                break;
                            case "[object String]":
                                confirmOption.ContentType = "application/x-www-form-urlencoded";
                                convertContent = confirmOption.Content;
                                break;
                            case "[object Object]":
                                confirmOption.ContentType = "application/x-www-form-urlencoded";
                                convertContent = this.__Tools.objectConvertPostBody(confirmOption.Content);
                                break;
                            case "[object Array]":
                                confirmOption.ContentType = "application/x-www-form-urlencoded";
                                convertContent = this.__Tools.objectConvertPostBody(confirmOption.Content);
                                break;
                            case "[object ArrayBuffer]":
                                confirmOption.ContentType = "application/x-www-form-urlencoded";
                                convertContent = this.__Tools.objectConvertPostBody(confirmOption.Content);
                                break;
                            default:
                                confirmOption.ContentType = "auto";
                                break;
                        }
                        switch (confirmOption.ContentType) {
                            case "multipart/form-data":
                                this.__XMLInstance.open(confirmOption.ReqType, confirmOption.Address, confirmOption.Async, confirmOption.UserName, confirmOption.PassWord);
                                this.__Tools.requestHeaderProcess.call(this, confirmOption.ExtraHeader);
                                switch (this.__config.BrowseEnviron) {
                                    case "CHROME":
                                        if(confirmOption.ResType){
                                            this.__XMLInstance.responseType = confirmOption.ResType;
                                        }
                                        break;
                                }
                                if (confirmOption.Async) {
                                    if (this.__config.BrowseEnviron === "IE") {
                                        this.__XMLInstance.onreadystatechange = function () {
                                            if (curIns.__XMLInstance.readyState === 4) {
                                                readyChangeProcess();
                                            }
                                        }
                                    } else {
                                        this.__Tools.eventInitial.call(curIns, curIns, confirmOption);
                                    }
                                }
                                this.__XMLInstance.send(confirmOption.Content);
                                if (!confirmOption.Async) {
                                    readyChangeProcess();
                                    return FinalResponse;
                                }
                                break;
                            case "application/x-www-form-urlencoded":
                                switch (confirmOption.SendWays) {
                                    case "query":
                                        reqBody = this.__Tools.objectConvertPostBody(confirmOption.Content);
                                        collAddress = addressAnalyse[1];
                                        if (collAddress) {
                                            if (reqBody !== "") {
                                                collAddress = collAddress + "&" + reqBody;
                                            }
                                        } else {
                                            collAddress = "";
                                            if (reqBody !== "") {
                                                collAddress = reqBody;
                                            }
                                        }
                                        if (collAddress !== "") {
                                            collAddress = addressAnalyse[0] + "?" + collAddress;
                                            this.__XMLInstance.open(
                                                String(confirmOption.ReqType).toLowerCase(),
                                                collAddress,
                                                confirmOption.Async,
                                                confirmOption.UserName,
                                                confirmOption.PassWord
                                            )
                                        } else {
                                            this.__XMLInstance.open(
                                                String(confirmOption.ReqType).toLowerCase(),
                                                addressAnalyse[0],
                                                confirmOption.Async,
                                                confirmOption.UserName,
                                                confirmOption.PassWord
                                            )
                                        }
                                        this.__Tools.requestHeaderProcess.call(this, confirmOption.ExtraHeader);
                                        switch (this.__config.BrowseEnviron) {
                                            case "CHROME":
                                                if(confirmOption.ResType){
                                                    this.__XMLInstance.responseType = confirmOption.ResType;
                                                }
                                                break;
                                        }
                                        if (confirmOption.Async) {
                                            switch (this.__config.BrowseEnviron) {
                                                case "CHROME":
                                                    this.__Tools.eventInitial.call(curIns, curIns, confirmOption);
                                                    break;
                                                case "IE":
                                                    this.__XMLInstance.onreadystatechange = function () {
                                                        if (curIns.__XMLInstance.readyState === 4) {
                                                            readyChangeProcess();
                                                        }
                                                    }
                                                    break;
                                            }
                                        }
                                        this.__XMLInstance.send();
                                        if (!confirmOption.Async) {
                                            readyChangeProcess();
                                            return FinalResponse;
                                        }
                                        break;
                                    case "body":
                                        collAddress = this.__Tools.objectConvertPostBody(confirmOption.Content);
                                        reqBody = addressAnalyse[1];
                                        if (reqBody) {
                                            if (collAddress) {
                                                reqBody = reqBody + "&" + collAddress;
                                            }
                                        } else {
                                            reqBody = "";
                                            if (collAddress !== "") {
                                                reqBody = collAddress;
                                            }
                                        }
                                        this.__XMLInstance.open(
                                            String(confirmOption.ReqType).toLowerCase(),
                                            addressAnalyse[0],
                                            confirmOption.Async,
                                            confirmOption.UserName,
                                            confirmOption.PassWord
                                        );
                                        this.__Tools.requestHeaderProcess.call(this, confirmOption.ExtraHeader);
                                        switch (this.__config.BrowseEnviron) {
                                            case "CHROME":
                                                if(confirmOption.ResType){
                                                    this.__XMLInstance.responseType = confirmOption.ResType;
                                                }
                                                break;
                                        }
                                        this.__XMLInstance.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                        if (confirmOption.Async) {
                                            switch (this.__config.BrowseEnviron) {
                                                case "CHROME":
                                                    this.__Tools.eventInitial.call(curIns, curIns, confirmOption);
                                                    break;
                                                case "IE":
                                                    this.__XMLInstance.onreadystatechange = function () {
                                                        if (curIns.__XMLInstance.readyState === 4) {
                                                            readyChangeProcess();
                                                        }
                                                    }
                                                    break;
                                            }
                                        }
                                        this.__XMLInstance.send(reqBody);
                                        if (!confirmOption.Async) {
                                            readyChangeProcess();
                                            return FinalResponse;
                                        }
                                        break;
                                    default:
                                        switch (String(confirmOption.ReqType).toLowerCase()) {
                                            case "get":
                                                if (addressAnalyse.length === 2) {
                                                    collAddress = addressAnalyse[0];
                                                    collAddress = collAddress + "?" + addressAnalyse[1];
                                                    collAddress = collAddress + "&" + convertContent;
                                                } else {
                                                    collAddress = addressAnalyse[0];
                                                    collAddress = collAddress + "?" + convertContent;
                                                }
                                                this.__XMLInstance.open(confirmOption.ReqType, collAddress, confirmOption.Async, confirmOption.UserName, confirmOption.PassWord);
                                                this.__Tools.requestHeaderProcess.call(this, confirmOption.ExtraHeader);
                                                break;
                                            case "post":
                                                collAddress = addressAnalyse[0];
                                                reqBody = addressAnalyse[1];
                                                this.__XMLInstance.open(confirmOption.ReqType, collAddress, confirmOption.Async, confirmOption.UserName, confirmOption.PassWord);
                                                this.__XMLInstance.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                                this.__Tools.requestHeaderProcess.call(this, confirmOption.ExtraHeader);
                                                if (reqBody) {
                                                    if (String(reqBody).length > 0) {
                                                        reqBody = reqBody + "&" + convertContent;
                                                    } else {
                                                        reqBody = convertContent;
                                                    }
                                                } else {
                                                    reqBody = convertContent;
                                                }
                                                break;
                                            default:
                                                throw new Error("暂未支持该请求方式的请求处理");
                                        }
                                        switch (this.__config.BrowseEnviron) {
                                            case "CHROME":
                                                if(confirmOption.ResType){
                                                    this.__XMLInstance.responseType = confirmOption.ResType;
                                                }
                                                break;
                                        }
                                        if (confirmOption.Async) {
                                            switch (this.__config.BrowseEnviron) {
                                                case "IE":
                                                    this.__XMLInstance.onreadystatechange = function () {
                                                        if (curIns.__XMLInstance.readyState === 4) {
                                                            readyChangeProcess();
                                                        }
                                                    }
                                                    break;
                                                case "CHROME":
                                                    this.__Tools.eventInitial.call(this, curIns, confirmOption);
                                                    break;
                                            }
                                        }
                                        switch (String(confirmOption.ReqType).toLowerCase()) {
                                            case "get":
                                                this.__XMLInstance.send();
                                                break;
                                            case "post":
                                                this.__XMLInstance.send(reqBody);
                                                break;
                                        }
                                        if (!confirmOption.Async) {
                                            readyChangeProcess();
                                            return FinalResponse;
                                        }
                                        break;
                                }
                                break;
                        }
                    } else {
                        throw new Error("选项参数必须是对象");
                    }
                } else {
                    throw new Error("非法调用此函数");
                }
            }
        },
        abortAnyRequest: function () {
            if (Object.prototype.toString.call(this.__XMLInstance) === "[object XMLHttpRequest]") {
                this.__XMLInstance.abort();
            } else if (Object.prototype.toString.call(this.__XMLInstance) === "[object Object]") {
                if (Object.prototype.toString.call(this.__XMLInstance.abort) === "[object Function]") {
                    this.__XMLInstance.abort();
                }
            }
        },
        GetRequest: function (Address, Content, CallBack, extraHeader, responseType, ASync, UserName, PassWord) {
            var result = this.__Tools.RequestProcess.call(this, {
                Address: Address,
                Content: Content,
                ReqType: "get",
                ResType: responseType,
                SendWays: "query",
                CallBack: CallBack,
                Async: ASync,
                ExtraHeader: extraHeader,
                UserName: UserName,
                PassWord: PassWord
            });
            return result;
        },
        PostRequest: function (Address, Content, CallBack, extraHeader, responseType, ASync, UserName, PassWord) {
            var result = this.__Tools.RequestProcess.call(this, {
                Address: Address,
                Content: Content,
                ReqType: "post",
                ResType: responseType,
                SendWays: "body",
                CallBack: CallBack,
                Async: ASync,
                ExtraHeader: extraHeader,
                UserName: UserName,
                PassWord: PassWord
            });
            return result;
        }
    }
    if (Object.prototype.toString.call(Configure) === "[object Object]" && InstanceObject.__Tools.detectObject(Configure).length > 0) {
        for (var num in Configure) {
            switch (String(num).toLowerCase()) {
                case "enableasync":
                    if (Object.prototype.toString.call(Configure[num]) === "[object Boolean]") {
                        InstanceObject.__config.EnableAsync = Configure[num];
                    }
                    break;
                case "timeout":
                    if (isNaN(Configure[num]) === false && isFinite(Configure[num]) && parseInt(Configure[num]) >= 0) {
                        InstanceObject.__config.timeout = Configure[num];
                    }
                    break;
                case "withcredentials":
                    if (Object.prototype.toString.call(Configure[num]) === "[object Boolean]") {
                        InstanceObject.__config.withCredentials = Configure[num];
                    }
                    break;
                case "username":
                    if (String(Configure[num]).length > 0) {
                        InstanceObject.__config.userName = InstanceObject.__Tools.lightCopy(String(Configure[num]).toString());
                    } else {
                        InstanceObject.__config.userName = null;
                    }
                    break;
                case "password":
                    if (String(Configure[num]).length > 0) {
                        InstanceObject.__config.passWord = InstanceObject.__Tools.lightCopy(String(Configure[num]).toString());
                    } else {
                        InstanceObject.__config.passWord = null;
                    }
                    break;
            }
        }
    }
    try {
        /**尝试使用Internet Explorer AJAX*/
        InstanceObject.__XMLInstance = new ActiveXObject("Microsoft.XMLHTTP");
        InstanceObject.__config.BrowseEnviron = "IE";
    } catch (e) {
        /**使用chrome AJAX*/
        InstanceObject.__XMLInstance = new XMLHttpRequest();
        InstanceObject.__XMLInstance.withCredentials = InstanceObject.__config.withCredentials;
        if (InstanceObject.__config.EnableAsync) {
            InstanceObject.__XMLInstance.timeout = InstanceObject.__config.timeout;
        }
        InstanceObject.__config.BrowseEnviron = "CHROME";
    }
    return InstanceObject;
}

/**@deprecated
 * AJAX请求处理构建实例
 *
 * Configure：参数对象
 * EnableAsync：启用异步请求模式，IE浏览器下默认强制为false，不启用
 * TimeOut：设置请求超时的时间
 * 示例：
 * var test= AJAX_RequestProcess({EnableAsync:true,TimeOut:15000});
 *
 * 本实例共有GetRequest（Get 请求）、PostRequest（Post 请求）、abortAnyRequest（中断请求）三个函数
 * 用法：
 *
 * GET
 * test.GetRequest(需要请求的网址,请求体数据,回调函数);
 *
 * POST
 * test.PostRequest(需要请求的网址,请求体数据,回调函数);
 *
 * 中断请求
 * test.abortAnyRequest();
 *
 * 回调函数的传参在异步请求模式下是一个包含两个数据的数组，第一个是请求的状态（ready、abort、error、timeout），第二个是请求后响应的数据。
 * 非异步模式下传参是请求后得到的响应数据
 *
 * 示例：test.GetRequest(需要请求的网址,请求体数据,function(value){
 *     switch(Object.prototype.toString.call(value)){
 *         case "[object Array]":
 *         switch(value[0]){
 *             case "ready":
 *             console.log("这是请求成功的");
 *             break;
 *             case "abort":
 *             console.log("这是请求放弃的");
 *             break;
 *             case "error":
 *             console.log("这是请求错误的");
 *             break;
 *             case "timeout":
 *             console.log("这是请求超时的");
 *             break;
 *         }
 *         break;
 *         default:
 *         console.log(value)
 *         break;
 *     }
 * })
 *
 * 非异步模式下可这样获取响应数据
 *
 * var pk = test.PostRequest(需要请求的网址,请求体数据);
 *
 * 以上示例均可在 GetRequest、PostRequest 函数中通用
 *
 * 本AJAX请求处理构建实例函数可完美兼容至 Internet Explorer 6.0 （IE6）浏览器使用
 *
 * */
function oldAJAX_RequestProcess(Configure) {
    function notEmptyDetect(data) {
        var detectValue;
        if (Object.prototype.toString.call(data) === "[object Array]") {
            if (data.length > 0) {
                detectValue = true;
            } else {
                detectValue = false;
            }
        } else if (Object.prototype.toString.call(data) === "[object Object]") {
            var count = 0;
            for (var num in data) {
                count++;
                break;
            }
            if (count > 0) {
                detectValue = true;
            } else {
                detectValue = false;
            }
        }
        return detectValue;
    };
    var InstanceObject = {
        "__XMLInstance": undefined,
        "__config": {timeout: 0, EnableAsync: false, BrowseEnviron: "IE"},
        "__Tools": {
            objectConvertPostBody: function (objectData) {
                var FinalConvert = [];
                if (Object.prototype.toString.call(objectData) === "[object Object]") {
                    for (var num in objectData) {
                        if (FinalConvert.length > 0) {
                            FinalConvert.push("&");
                        }
                        FinalConvert.push(num);
                        FinalConvert.push("=");
                        FinalConvert.push(objectData[num]);
                    }
                }
                return FinalConvert.join("");
            }
        },
        GetRequest: function (RequestAddress, RequestContent, callback) {
            var FinalResponse;
            var curIns = this;
            try {
                var convertData;
                var collAddress;
                switch (this.__config.BrowseEnviron) {
                    case "CHROME":
                        if (this.__config.EnableAsync) {
                            this.__XMLInstance.timeout = this.__config.timeout;
                        }
                        break;
                }
                if (Object.prototype.toString.call(RequestContent) === "[object Object]") {
                    convertData = this.__Tools.objectConvertPostBody(RequestContent);
                }
                if (convertData !== undefined && String(convertData).length > 0) {
                    collAddress = RequestAddress + "?" + convertData;
                } else {
                    collAddress = convertData;
                }
                if (collAddress === undefined) {
                    this.__XMLInstance.open("get", RequestAddress, this.__config.EnableAsync);
                } else {
                    this.__XMLInstance.open("get", collAddress, this.__config.EnableAsync);
                }
                this.__XMLInstance.send();
                if (this.__config.EnableAsync) {
                    var execNum = 1
                    FinalResponse = [];
                    curIns.__XMLInstance.onloadend = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("ready");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.onerror = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("error");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.onabort = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("abort");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.ontimeout = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("timeout");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                } else {
                    FinalResponse = this.__XMLInstance.responseText;
                    if (typeof callback === "function") {
                        callback(FinalResponse);
                    }
                }
            } catch (e) {
                if (typeof callback === "function") {
                    callback(e);
                } else {
                    FinalResponse = e;
                }
            }
            if (!this.__config.EnableAsync) {
                return FinalResponse;
            }
        },
        PostRequest: function (RequestAddress, RequestContent, callback) {
            var curIns = this;
            var FinalResponse;
            try {
                var convertData;
                switch (this.__config.BrowseEnviron) {
                    case "CHROME":
                        if (this.__config.EnableAsync) {
                            this.__XMLInstance.timeout = this.__config.timeout;
                        }
                        break;
                }
                this.__XMLInstance.open("post", RequestAddress, this.__config.EnableAsync);
                if (Object.prototype.toString.call(RequestContent) === "[object Object]" || Object.prototype.toString.call(RequestContent) === "[object String]") {
                    this.__XMLInstance.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    switch (Object.prototype.toString.call(RequestContent)) {
                        case "[object Object]":
                            convertData = this.__Tools.objectConvertPostBody(RequestContent);
                            this.__XMLInstance.send(convertData);
                            break;
                        case "[object String]":
                            this.__XMLInstance.send(RequestContent);
                            break;
                    }
                } else {
                    this.__XMLInstance.send(RequestContent);
                }
                if (this.__config.EnableAsync) {
                    FinalResponse = [];
                    var execNum = 1;
                    curIns.__XMLInstance.onloadend = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("ready");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.onerror = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("error");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.onabort = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("abort");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                    curIns.__XMLInstance.ontimeout = function (value) {
                        if (execNum > 1) {
                            return;
                        }
                        execNum++;
                        FinalResponse.push("timeout");
                        FinalResponse.push(curIns.__XMLInstance.responseText);
                        if (typeof callback === "function") {
                            callback(FinalResponse);
                        } else {
                            return FinalResponse;
                        }
                    }
                } else {
                    FinalResponse = this.__XMLInstance.responseText;
                    if (typeof callback === "function") {
                        callback(FinalResponse);
                    }
                }
            } catch (e) {
                if (typeof callback === "function") {
                    callback(e);
                } else {
                    FinalResponse = e;
                }
            }
            if (!this.__config.EnableAsync) {
                return FinalResponse;
            }
        },
        abortAnyRequest: function () {
            this.__XMLInstance.abort();
        }
    }
    if (Object.prototype.toString.call(Configure) === "[object Object]" && notEmptyDetect(Configure)) {
        for (var num in Configure) {
            switch (String(num).toLowerCase()) {
                case "enableasync":
                    if (Object.prototype.toString.call(Configure[num]) === "[object Boolean]") {
                        InstanceObject.__config.EnableAsync = Configure[num];
                    }
                    break;
                case "timeout":
                    if (isNaN(Configure[num]) === false && isFinite(Configure[num]) && parseInt(Configure[num]) >= 0) {
                        InstanceObject.__config.timeout = Configure[num];
                    }
                    break;
            }
        }
    }
    try {
        /**尝试使用Internet Explorer AJAX*/
        InstanceObject.__XMLInstance = new ActiveXObject("Microsoft.XMLHTTP");
        InstanceObject.__config.EnableAsync = false;
    } catch (e) {
        /**使用chrome AJAX*/
        InstanceObject.__XMLInstance = new XMLHttpRequest();
        InstanceObject.__config.BrowseEnviron = "CHROME";
    }
    return InstanceObject;
}

/**表单请求器*/
function FormRequester(option) {

    function detObj(source) {
        var Final = {length: 0, PropertyItem: []}
        try {
            for (var item in source) {
                Final.length++;
                Final.PropertyItem.push(item);
            }
        } catch (e) {
            return false;
        }
        return Final;
    }

    function eleCreate(objectSource) {
        try {
            var Template = {type: "text", name: "", value: "", src: "", checked: ""}
            var FinalElement;
            if (detObj(objectSource).length > 0) {
                for (var chkItem in objectSource) {
                    switch (String(chkItem).toLowerCase()) {
                        case "name":
                            Template.name = objectSource[chkItem];
                            break;
                        case "type":
                            Template.type = objectSource[chkItem];
                            break;
                        case "value":
                            Template.value = objectSource[chkItem];
                            break;
                        case "src":
                            Template.src = objectSource[chkItem];
                            break;
                        case "checked":
                            if (Object.prototype.toString.call(objectSource[chkItem]) === "[object Boolean]") {
                                if (objectSource[chkItem]) {
                                    Template.checked = "checked";
                                }
                            }
                            break;
                        default:
                            Template[chkItem] = objectSource[chkItem];
                            break;
                    }
                }
                if (detObj(Template).length >= 4 || (Template.name !== "" || Template.value !== "" || Template.src !== "")) {
                    FinalElement = document.createElement("input");
                    for (var intItem in Template) {
                        if (Template[intItem] !== "") {
                            FinalElement.setAttribute(intItem, Template[intItem]);
                        }
                    }
                    return FinalElement;
                }
            } else {
                return undefined;
            }
        } catch (e) {
            return undefined
        }
    }

    if (Object.prototype.toString.call(option) === "[object Object]") {
        var FormConfig = {
            method: "GET",
            "accept-charset": "UTF-8",
            enctype: "application/x-www-form-urlencoded",
            action: window.location.href
        }
        var OperateOption = {
            OnlyMake: false
        }
        var FormItem = [];
        if (detObj(option).length > 0) {
            for (var item in option) {
                var createNew;
                switch (String(item).toLowerCase()) {
                    case "onlymake":
                        if (Object.prototype.toString.call(option[item]) === "[object Boolean]") {
                            OperateOption.OnlyMake = option[item];
                        }
                        break;
                    case "method":
                        FormConfig.method = option[item];
                        break;
                    case "acceptcharset":
                        FormConfig["accept-charset"] = option[item];
                        break;
                    case "accept-charset":
                        FormConfig["accept-charset"] = option[item];
                        break;
                    case "enctype":
                        FormConfig.enctype = option[item];
                        break;
                    case "action":
                        FormConfig.action = option[item];
                        break;
                    case "target":
                        FormConfig.target = option[item];
                        break;
                    case "formitem":
                        var finEle;
                        switch (Object.prototype.toString.call(option[item])) {
                            case "[object Object]":
                                if (detObj(option[item]).length > 0) {
                                    for (var ctItem in option[item]) {
                                        switch (Object.prototype.toString.call(option[item][ctItem])) {
                                            case "[object Object]":
                                                finEle = eleCreate(option[item][ctItem]);
                                                if (finEle) {
                                                    FormItem.push(finEle);
                                                }
                                                break;
                                            case "[object String]":
                                                finEle = document.createElement("input");
                                                finEle.setAttribute("name", ctItem);
                                                finEle.setAttribute("type", "text");
                                                finEle.setAttribute("value", option[item][ctItem]);
                                                FormItem.push(finEle);
                                                break;
                                            case "[object Number]":
                                                finEle = document.createElement("input");
                                                finEle.setAttribute("name", ctItem);
                                                finEle.setAttribute("type", "number");
                                                finEle.setAttribute("value", option[item][ctItem]);
                                                FormItem.push(finEle);
                                                break;
                                            case "[object Boolean]":
                                                finEle = document.createElement("input");
                                                finEle.setAttribute("name", ctItem);
                                                finEle.setAttribute("type", "checkbox");
                                                if (option[item][ctItem]) {
                                                    finEle.setAttribute("checked", "checked");
                                                }
                                                FormItem.push(finEle);
                                                break;
                                            default:
                                                if (String(Object.prototype.toString.call(option[item][ctItem])).includes("HTML") && String(Object.prototype.toString.call(option[item][ctItem])).includes("Element")) {
                                                    FormItem.push(option[item][ctItem]);
                                                }
                                                break;
                                        }
                                    }
                                }
                                break;
                            case "[object Array]":
                                if (option[item].length > 0) {
                                    for (var arrChk in option[item]) {
                                        switch (Object.prototype.toString.call(option[item][arrChk])) {
                                            case "[object Object]":
                                                finEle = eleCreate(option[item][arrChk]);
                                                if (finEle) {
                                                    FormItem.push(finEle);
                                                }
                                                break;
                                            default:
                                                if (String(Object.prototype.toString.call(option[item][arrChk])).includes("HTML") && String(Object.prototype.toString.call(option[item][arrChk])).includes("Element")) {
                                                    FormItem.push(option[item][arrChk]);
                                                }
                                                break;
                                        }
                                    }
                                }
                                break;
                        }
                        break;
                    default:
                        if (Object.prototype.toString.call(option[item]) === "[object Object]") {
                            var getEle = eleCreate(option[item]);
                            if (getEle) {
                                FormItem.push(getEle);
                            }
                        } else if (Object.prototype.toString.call(option[item]) === "[object String]") {
                            createNew = document.createElement("input");
                            createNew.setAttribute("name", item);
                            createNew.setAttribute("type", "text");
                            createNew.setAttribute("value", option[item]);
                            FormItem.push(createNew);
                        } else if (Object.prototype.toString.call(option[item]) === "[object Number]" && isFinite(option[item]) && !isNaN(option[item])) {
                            createNew = document.createElement("input");
                            createNew.setAttribute("name", item);
                            createNew.setAttribute("type", "number");
                            createNew.setAttribute("value", option[item]);
                            FormItem.push(createNew);
                        } else if (Object.prototype.toString.call(option[item]) === "[object Boolean]") {
                            finEle = document.createElement("input");
                            finEle.setAttribute("name", item);
                            finEle.setAttribute("type", "checkbox");
                            if (option[item]) {
                                finEle.setAttribute("checked", "checked");
                            }
                            FormItem.push(finEle);
                        } else {
                            if (String(Object.prototype.toString.call(option[item])).includes("HTML") && String(Object.prototype.toString.call(option[item])).includes("Element")) {
                                FormItem.push(option[item]);
                            }
                        }
                        break;
                }
            }
            var FormElement = document.createElement("form");
            FormElement.setAttribute("style", "display:none");
            for (var initSetting in FormConfig) {
                if (FormConfig[initSetting] !== "") {
                    FormElement.setAttribute(initSetting, FormConfig[initSetting]);
                }
            }
            for (var childElement in FormItem) {
                FormElement.appendChild(FormItem[childElement]);
            }
            if (OperateOption.OnlyMake) {
                return FormElement;
            } else {
                document.body.appendChild(FormElement);
                FormElement.submit();
            }
        } else {
            throw new Error("收到的传参是空对象");
        }
    } else {
        throw new Error("必须要传入选项数据");
    }
}

export {AJAX_RequestProcess, oldAJAX_RequestProcess, FormRequester};